package controller

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/String"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/httpserver"
	"go-fox-edge-kernel-manager/repository"
	"go-fox-edge-kernel-manager/utils/PageUtils"
	"strconv"
	"strings"
)

type repoLocalJsnModelListController struct {
}

func (e *repoLocalJsnModelListController) init() {
	httpserver.Server.POST("/repository/local/jsn-list/page", e.selectPageList)
	httpserver.Server.GET("/repository/local/jsn-list/entity", e.queryEntity)
	httpserver.Server.POST("/repository/local/jsn-list/entity", e.insertEntity)
	httpserver.Server.PUT("/repository/local/jsn-list/entity", e.updateEntity)
	httpserver.Server.DELETE("/repository/local/jsn-list/entity", e.deleteEntity)
	httpserver.Server.DELETE("/repository/local/jsn-list/entities", e.deleteEntityList)
}

func (e *repoLocalJsnModelListController) selectPageList(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	id := Map.GetInt64(body, "id", 0)

	// 简单校验参数
	if Map.HasNull(body, "id") {
		return nil, errors.New("参数不能为空: id")
	}

	// 获得组件信息
	compEntity := repository.LocalCompService.GetRepoCompEntity(id)
	if compEntity == nil {
		return nil, errors.New("找不到对应的组件: " + String.ToString(id))
	}

	// 查询数据
	entityList := repository.LocalJsnModel.GetDeviceModelEntityList(compEntity)

	// 分页返回
	return PageUtils.GetPageList(entityList, body), nil
}

func (e *repoLocalJsnModelListController) queryEntity(context httpserver.Context) (interface{}, error) {
	str := context.Query("id")
	if str == "" {
		return nil, errors.New("输入的id为null")
	}

	id, err := strconv.ParseInt(str, 10, 64)
	if err != nil {
		return nil, err
	}

	exist := repository.LocalJsnModel.QueryEntity(id)
	if exist == nil {
		return nil, errors.New("实体不存在")
	}

	return commEntity.BuildMap(exist), nil
}

func (e *repoLocalJsnModelListController) insertEntity(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	delete(body, "id")
	return e.insertOrUpdate(context, body)
}

func (e *repoLocalJsnModelListController) updateEntity(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return e.insertOrUpdate(context, body)
}

func (e *repoLocalJsnModelListController) insertOrUpdate(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	return nil, repository.LocalJsnModel.InsertOrUpdate(body)
}

func (e *repoLocalJsnModelListController) deleteEntity(context httpserver.Context) (interface{}, error) {
	str := context.Query("id")
	if str == "" {
		return nil, errors.New("输入的id为null")
	}

	id, err := strconv.ParseInt(str, 10, 64)
	if err != nil {
		return nil, err
	}

	exist := commEntityManager.GetEntityById(edgeEntity.Type.DeviceModelEntity.Type(), id)
	if exist == nil {
		return nil, errors.New("实体不存在")
	}

	err = commEntityManager.DeleteEntity(exist)
	if err != nil {
		return nil, err
	}

	return commEntity.BuildMap(exist), nil
}

func (e *repoLocalJsnModelListController) deleteEntityList(context httpserver.Context) (interface{}, error) {
	ids := context.Query("ids")

	idList := strings.Split(ids, ",")
	for _, str := range idList {
		if str == "" {
			continue
		}
		id, err := strconv.ParseInt(str, 10, 64)
		if err != nil {
			return nil, err
		}

		exist := commEntityManager.GetEntityById(edgeEntity.Type.DeviceModelEntity.Type(), id)
		if exist == nil {
			return nil, errors.New("实体不存在")
		}

		err = commEntityManager.DeleteEntity(exist)
		if err != nil {
			return nil, err
		}
	}

	return nil, nil
}
